<template>
<van-cell class="comment-item">
    <van-image slot="icon" class="avatar" round fit="cover" :src="comment.aut_photo" />
    <div slot="title" class="title-wrap">
        <div class="user-name">{{comment.aut_name}}</div>
        <van-button class="like-btn" :icon="comment.is_liking?'good-job':'good-job-o'" :style="{color:comment.is_liking?'#dada1f':'inherit'}" :loading="loading" @click="onLike">{{comment.like_count === 0 ?'赞':comment.like_count}}</van-button>
    </div>

    <div slot="label">
        <p class="comment-content">{{comment.content}}</p>
        <div class="bottom-info">
            <span class="comment-pubdate">{{comment.pubdate | relativeTime}}</span>
            <van-button class="reply-btn" round @click="$emit('reply-click',comment)">回复 {{comment.reply_count}}</van-button>
        </div>
    </div>
</van-cell>
</template>

<script>
import { addLike, deleteLike } from '@/api/comment' // eslint-disable-line
export default {
  name: 'CommentItem',
  components: {},
  props: {
    comment: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      loading: false// 默认不显示加载中效果
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onLike () {
      this.loading = true
      try {
        if (this.comment.is_liking) {
        // 已点赞 --> 取消点赞
          console.log('取消点赞')
          await deleteLike(this.comment.com_id)
          // 单向数据流 如果传递的props是基本数据类型(number,string,boolean) 事件 但是如果是引用类型 可以直接在子组件中修改
        } else {
        // 未点赞 --> 点赞
          console.log('点赞')
          await addLike(this.comment.com_id)
        }
      } catch (err) {

      }
      if (this.comment.is_liking) {
        this.comment.like_count--
      } else {
        this.comment.like_count++
      }
      this.comment.is_liking = !this.comment.is_liking
      this.loading = false
    }
  }
}
</script>

<style lang="less" scoped>
.comment-item {
    .avatar {
        width: 72px;
        height: 72px;
        margin-right: 25px;
    }

    .title-wrap {
        display: flex;
        justify-content: space-between;
        align-items: center;

        .user-name {
            color: #406599;
            font-size: 26px;
        }
    }

    .comment-content {
        font-size: 32px;
        color: #222222;
        word-break: break-all;
        text-align: justify;
    }

    .comment-pubdate {
        font-size: 19px;
        color: #222;
        margin-right: 25px;
    }

    .bottom-info {
        display: flex;
        align-items: center;
    }

    .reply-btn {
        width: 135px;
        height: 48px;
        line-height: 48px;
        font-size: 21px;
        color: #222;
    }

    .like-btn {
        height: 30px;
        padding: 0;
        border: none;
        font-size: 19px;
        line-height: 30px;
        margin-right: 7px;

        .van-icon {
            font-size: 30px;
        }
    }
}
</style>
